package cn.com.utt.unms.controller;

import java.io.File;
import java.security.Principal;
import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import cn.com.utt.common.vo.RestResult;
import cn.com.utt.common.vo.TableInput;
import cn.com.utt.common.vo.TableOutput;
import cn.com.utt.common.vo.User;
import cn.com.utt.unms.pojo.AccountConfig;
import cn.com.utt.unms.service.AccountConfigService;
import cn.com.utt.unms.service.DeviceService;
import cn.com.utt.unms.vo.AccountConfigInput;

/**
 * 固定账号
 * 
 * @param wifiConf
 * @param principal
 * @return
 */
@RestController
@RequestMapping("v1/accountConfig")
public class AccountConfigController {
	
	@Autowired
	private AccountConfigService accountConfigService;
	
	@Autowired
	private DeviceService deviceService;

	@RequestMapping(value = "/list", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> accountConfigList(Principal principal, @RequestBody TableInput input) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		TableOutput<AccountConfig> output = accountConfigService.queryAccounConfigList(input, user.getId());
		result.put("start", output.getStart());
		result.put("rows", output.getRows());
		result.put("total", output.getTotal());
		result.put("list", output.getList());
		return result.toJsonObject();
	}

	@RequestMapping(value = "/addOrEdit", method = { RequestMethod.POST }, consumes = "application/json")
	public Map<String, Object> addAccountConfig(Principal principal,
			@RequestBody AccountConfigInput accountConfigInput) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		result = accountConfigService.addAccountConfig(accountConfigInput.getAccountConfig(),
				accountConfigInput.getType(), user.getId());
		System.out.println(result.toJsonObject());
		return result.toJsonObject();
	}

	@RequestMapping(value = "/del", method = { RequestMethod.POST })
	public Map<String, Object> editAccountConfig(Principal principal, @RequestParam(value = "ids[]") List<Long> ids) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		result = accountConfigService.delAccountConfig(ids, user.getId());
		return result.toJsonObject();
	}

	@RequestMapping(value = "/export", method = { RequestMethod.POST })
	public Map<String, Object> exportAccountConfig(Principal principal) {
		RestResult result = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		result = accountConfigService.exportAccountConfig(user.getId());
		System.out.println(result.toJsonObject());
		return result.toJsonObject();
	}

	@RequestMapping(value = "/import", method = { RequestMethod.POST })
	@ResponseBody
	public Map<String, Object> importAccountConfig(@RequestParam("file") MultipartFile multipartFile,
			Principal principal) {
		RestResult result = deviceService.writeFileToDisk(multipartFile, "excel");
		if (result.getErrorCode() != 0) {
			return result.toJsonObject();
		}
		String filename = result.getErrorMsg();
		File f = new File(filename);
		RestResult res = new RestResult();
		Authentication authentication = (Authentication) principal;
		User user = (User) authentication.getPrincipal();
		res = accountConfigService.importAccountConfig(f, user.getId());
		System.out.println(res.toJsonObject());
		return res.toJsonObject();
	}

}
